package com.shujia.reflect;

import java.io.BufferedReader;
import java.io.FileReader;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;

public class DataRead {

    public static <T> ArrayList<T> read(String path, Class<T> clazz) {

        ArrayList<T> list = new ArrayList<>();

        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(path));
            String line;
            while ((line = bufferedReader.readLine()) != null) {

                String[] values = line.split(",");

                //创建对象
                T t = clazz.newInstance();

                //给对象所有属性赋值
                //1、获取类所有属性
                Field[] fields = clazz.getDeclaredFields();

                for (int i = 0; i < fields.length; i++) {
                    Field field = fields[i];

                    //获取属性类型
                    Class<?> type = field.getType();

                    //获取属性名
                    String name = field.getName();

                    //不能直接通过属性赋值，因为之后public才能直接赋值
                    //只能通过set方法给属性赋值

                    //toUpperCase  转大写

                    //构建set方法
                    String monthName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);


                    if (type == Integer.class) {
                        //获取方法对象
                        Method method = clazz.getMethod(monthName, Integer.class);

                        //执行方法
                        method.invoke(t, Integer.parseInt(values[i]));

                    } else {
                        //获取方法对象
                        Method method = clazz.getMethod(monthName, String.class);

                        //执行方法
                        method.invoke(t, values[i]);

                    }
                }
                list.add(t);
            }

            bufferedReader.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }

}
